﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LTJ.Common;
using LTJ.FrameWork;
using LTJ.Entity;

namespace uswinweb.datacontrol
{
    public partial class RoutesList : System.Web.UI.UserControl
    {
        public DataTable routesTab = new DataTable();
        public string strWhere = " 1=1 ";
        public int pagesize = 10;
        public int pageindex = SASRequest.GetInt("currentPage", 1);
        public string action = SASRequest.GetString("action");
        public string parpms = SASRequest.GetString("parms").Trim();
        public string pageList = "";
        public string orderstr = " order by id asc ";
        public string routekey = SASRequest.GetString("routekey");
        public string shipcompkey = SASRequest.GetString("shipcompkey");
        public int cid = SASRequest.GetInt("cid", 0);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (action == "del")
            {
                int msg = Routes.DelRoutes(parpms);
            }
            if (action == "search")
            {
                if (routekey != "")
                {
                    string hxcodeList = "";
                    DataTable hxTab = BaseDatas.GetBaseDataListByStr(" code='searoute' and name like '%" + routekey + "%'");
                    foreach (DataRow dr in hxTab.Rows)
                    {
                        hxcodeList += "'" + dr["tcode"] + "',";
                    }
                    if (hxcodeList != "")
                    {
                        strWhere += " and tcode in (" + hxcodeList.Trim(',') + ")";
                    }
                }
                if (shipcompkey != "")
                {
                    string cgsList = "";
                    DataTable cgsTab = BaseDatas.GetBaseDataListByStr(" code='carrier' and name like '%" + routekey + "%'");
                    foreach (DataRow dr in cgsTab.Rows)
                    {
                        cgsList += "'" + dr["tcode"] + "',";
                    }
                    if (cgsList != "")
                    {
                        strWhere += " and bcode in (" + cgsList.Trim(',') + ")";
                    }
                }
            }

            if (action == "sort")
            {
                orderstr = parpms;
            }
            if (cid != 0)
            {
                strWhere += " and cid=" + cid;
            }
            int recordcount = 0;
            recordcount = Routes.GetRoutesCount(strWhere);
            routesTab = Routes.GetRoutesByPage(pageindex, pagesize, strWhere, orderstr);
            pageList = GetPageList(recordcount, pagesize, pageindex);
        }
        public string GetPageList(int recordcount, int pagesize, int currentpage)
        {
            if (SASRequest.GetInt("postnumber", 0) > 0)
            {
                return LTJ.Common.Utils.LuffyPage(recordcount, pagesize, currentpage, "RoutesList.ascx", "postnumber=" + SASRequest.GetInt("postnumber", 0), "showdata");
            }
            else
            {
                return LTJ.Common.Utils.LuffyPage(recordcount, pagesize, currentpage, "RoutesList.ascx", "cid="+cid+"&action=" + action + "&parms=" + parpms + "", "showdata");
            }
        }
        public string GetName(string pcode, string type)
        {
            string msg = "&nbsp;";
            string strWhere = " code='" + type.Trim() + "' and tcode='" + pcode.Trim() + "' ";
            BaseDataInfo binfo = BaseDatas.GetBaseDataByStr(strWhere);
            if (binfo != null)
            {
                msg = binfo.name;
            }
            return msg;
        }
    }
}